package com.heaboy.collection;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        class Student implements  Comparable<Student>{
            private int age;
            private String name;

            public Student(int age, String name) {
                this.age = age;
                this.name = name;
            }

            @Override
            public boolean equals(Object o) {
                if (this == o) return true;
                if (o == null || getClass() != o.getClass()) return false;
                Student student = (Student) o;
                return age == student.age && Objects.equals(name, student.name);
            }

            @Override
            public int hashCode() {
                return Objects.hash(age, name);
            }

            @Override
            public int compareTo(Student o) {
                return o.age-this.age;
            }

            @Override
            public String toString() {
                return "Student{" +
                        "age=" + age +
                        ", name='" + name + '\'' +
                        '}';
            }

            public int getAge() {
                return age;
            }

            public void setAge(int age) {
                this.age = age;
            }

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }
        }
        List<Student> studentList =new LinkedList<>();
        studentList.add(new Student(18,"zhangsan1"));
        studentList.add(new Student(19,"zhangsan2"));
        studentList.add(new Student(11,"zhangsan3"));
        studentList.add(new Student(11,"zhangsan3"));
        studentList.add(new Student(182,"zhangsan4"));
        studentList.add(new Student(183,"zhangsan5"));
        studentList.add(new Student(185,"zhangsan7"));
        studentList.add(new Student(184,"zhangsan6"));

        System.out.println(studentList.size());
        Set<Student> studentSet = new TreeSet<>(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.name.compareTo(o2.name);
            }
        });
        studentSet.addAll(studentList);
        System.out.println(studentSet.size());
        System.out.println(studentSet);
        Map<Integer,Student> map = new HashMap<>();
        for (Student student : studentList) {
            map.put(student.age,student);
        }
        Student student = map.get(18999);
        System.out.println(student);
        System.out.println(map.size());
        System.out.println(map);
        System.out.println("------------------------------");

        //list转为set
        Set<Student> studentSet1=studentList.stream().collect(Collectors.toCollection(
                ()->new TreeSet<>()
        ));
        System.out.println("list转换为set的studentset1"+studentSet1);
        System.out.println();
        //list转为map
        Map<Integer,Student> map1=studentList.stream()
                .collect(Collectors.toMap(
                        s->s.age,
                        s->s,
                        (existing,relacement)->existing,
                        LinkedHashMap::new
                ));
        System.out.println("list转换为map"+map1);
        System.out.println();

        //map转化为list
        List<Student> studentList1=map1.values().stream()
                .sorted(Comparator.comparing(s->s.name))
                .collect(Collectors.toList());
        System.out.println("map转化为list "+studentList1);
        System.out.println();

        //map转为set
        Set<Student> valueSet = map.values().stream()
                .collect(Collectors.toCollection(
                        () -> new TreeSet<>(Comparator.comparing(Student::getName))
                ));
        System.out.println("map转为set"+valueSet);
    }
}
